asm
  ld sp,$67FF
  end asm

#include "./library/pacmandelay.bas"
#include "./library/smsrnd.bas"

dim eee as uinteger at $6010
dim seed as uinteger at $6012
dim ee0 as uinteger at $6014
dim ee1 as uinteger at $6016
dim ee2 as uinteger at $6018
dim ee3 as uinteger at $601A
dim ex0 as uinteger at $601C
dim ey0 as uinteger at $601E

sub bagmanputchar(txp1 as uinteger,typ1 as uinteger,tch1 as ubyte,tat1 as ubyte):
  poke $9040+txp1*32+typ1 bxor 31,tch1
  poke $9840+txp1*32+typ1 bxor 31,tat1
  end sub

sub bagmanputsprite(tly2 as uinteger,txp2 as ubyte,typ2 as ubyte,tid2 as ubyte,tat2 as ubyte):
  poke $9800+(tly2*4),tid2
  poke $9801+(tly2*4),tat2
  poke $9802+(tly2*4),txp2 bxor $FF
  poke $9803+(tly2*4),typ2 bxor $FF
  end sub

function fastcall msxjoystick(vjo1 as ubyte) as ubyte:
  asm
    and 1
    rl a
    rl a
    rl a
    rl a
    rl a
    rl a
    ld b,a
    ld a,15
    out ($a0),a
    ld a,b
    out ($a1),a
    ld a,14
    out ($a0),a
    in a,($a2)
    and $3F
    ;xor $3F
    end asm
  end function

function fastcall joysticktest(vjo1 as ubyte) as ubyte:
  asm
    ld a,15
    out ($08),a
    ld a,64
    ld a,11
    out ($09),a
    ld a,14 ;!!!!!
    out ($08),a
    in a,($0C)
    xor $FF
    end asm
  end function








for ee0=0 to 31
  for ee1=0 to 27
    ee2=seed:seed=smsrnd(seed)
    ee2=seed:seed=smsrnd(seed)
    bagmanputchar(ee1,ee0,seed band 3,ee2)
    next:next

for ee0=0 to 15
  for ee1=0 to 15
    bagmanputchar(ee1+11,ee0+15,ee0*16+ee1,0)
    next:next

eee=0:ex0=8:ey0=8
do

bagmanputchar(1,1,48+((int(eee/10000)) mod 10),0)
bagmanputchar(2,1,48+((int(eee/1000)) mod 10),0)
bagmanputchar(3,1,48+((int(eee/100)) mod 10),0)
bagmanputchar(4,1,48+((int(eee/10)) mod 10),0)
bagmanputchar(5,1,48+(eee mod 10),0)

'out $08,$0A:ee0=in($0C)
ee0=joysticktest(0)
bagmanputchar(3,3,48+((int(ee0/100)) mod 10),0)
bagmanputchar(4,3,48+((int(ee0/10)) mod 10),0)
bagmanputchar(5,3,48+(ee0 mod 10),0)

ex0=ex0+((ee0 band 16)/16)-((ee0 band 8)/8)
ey0=ey0+((ee0 band 64)/64)-((ee0 band 32)/32)

bagmanputchar(ex0,ey0,eee band 127,0)

eee=eee+1
loop


'-------------------------------------------------------------------------------

do:loop

'eee=0
do
for ee0=0 to 31
  for ee1=0 to 31
    ee2=seed:seed=smsrnd(seed)
    ee2=seed:seed=smsrnd(seed)
    bagmanputchar(ee1,ee0,seed,ee2)
    ee2=seed:seed=smsrnd(seed)
    bagmanputsprite(0,ee1*2,ee0*2,ee0,ee0)
    pacmandelay(500)
    next:next
loop







eee=0
do
for ee0=0 to 31
  for ee1=0 to 31
    ee2=seed:seed=smsrnd(seed)
    ee2=seed:seed=smsrnd(seed)
    poke $9040+ee1*32+ee0 bxor 31,seed
    poke $9840+ee1*32+ee0 bxor 31,ee2
    pacmandelay(5000)
    ee2=seed:seed=smsrnd(seed)
    poke $9800+eee,seed
    eee=eee+1
    eee=eee band $3F
    poke $9804,eee  '- $9800=id, $9801=attr, $9802=x_inv, $9803=y_inv
    next:next
loop





do

for eee=$000 to $37F
  seed=smsrnd(seed):poke $9040+eee,seed
  seed=smsrnd(seed):poke $9840+eee,seed
  pacmandelay(500)
  next

loop






do

for eee=$9000 to $9BFF
  seed=smsrnd(seed)
  poke eee,seed
  pacmandelay(1000)
  next
for eee=$9800 to $981F
  seed=smsrnd(seed)
  poke eee,seed
  pacmandelay(1000)
  next

loop




'static ADDRESS_MAP_START( main_map, AS_PROGRAM, 8, bagman_state )
'AM_RANGE(0x0000, 0x5fff) AM_ROM
'AM_RANGE(0x6000, 0x67ff) AM_RAM
'AM_RANGE(0x9000, 0x93ff) AM_RAM_WRITE(videoram_w) AM_SHARE("videoram")
'AM_RANGE(0x9800, 0x9bff) AM_RAM_WRITE(colorram_w) AM_SHARE("colorram")
'AM_RANGE(0x9c00, 0x9fff) AM_WRITENOP    /* written to, but unused */
'AM_RANGE(0xa000, 0xa000) AM_READ(pal16r6_r)
'//AM_RANGE(0xa800, 0xa805) AM_READ(bagman_ls259_r) /*just for debugging purposes*/
'AM_RANGE(0xa000, 0xa000) AM_WRITE(irq_mask_w)
'AM_RANGE(0xa001, 0xa002) AM_WRITE(flipscreen_w)
'AM_RANGE(0xa003, 0xa003) AM_WRITEONLY AM_SHARE("video_enable")
'AM_RANGE(0xc000, 0xffff) AM_ROM /* Super Bagman only */
'AM_RANGE(0x9800, 0x981f) AM_WRITEONLY AM_SHARE("spriteram") /* hidden portion of color RAM */
'/* here only to initialize the pointer, */
'/* writes are handled by colorram_w */
'AM_RANGE(0xa800, 0xa805) AM_WRITE(ls259_w) /* TMS5110 driving state machine */
'AM_RANGE(0xa004, 0xa004) AM_WRITE(coincounter_w)
'AM_RANGE(0xb000, 0xb000) AM_READ_PORT("DSW")
'AM_RANGE(0xb800, 0xb800) AM_READNOP                             /* looks like watchdog from schematics */
'ADDRESS_MAP_END






'sub pingpongputsprite(tlayer as ubyte,tx as ubyte,ty as ubyte,tid as ubyte,tclut as ubyte):
'  poke $9006+((tlayer band 15)*4),tx  '- x sprite 0
'  poke $9004+((tlayer band 15)*4),ty bxor 255  '- y sprite 0
'  poke $9005+((tlayer band 15)*4),tid          '- shape sprite 0
'  poke $9003+((tlayer band 15)*4),tclut        '- clut? sprite 0
'  end sub
'
'function test1(t1 as uinteger,t2 as uinteger,t3 as uinteger,t4 as uinteger)  as uinteger
'  end function
'
'sub zpoke(t1 as uinteger,t2 as ubyte)
'  poke t1,t2
'  end sub
'
'sub putchar(tx1 as uinteger, ty1 as uinteger, tch1 as uinteger)
'  poke ($8400+(tx1*32)+ty1) bxor %0000001111100000,(tch1 band 255)
'  end sub
'
'pingpongputsprite(0,21,10,4,3)
'pingpongputsprite(1,21,30,4,3)
'pingpongputsprite(2,21,50,4,3)
'pingpongputsprite(3,21,70,4,3)
'pingpongputsprite(4,21,90,4,3)
'pingpongputsprite(5,21,110,4,3)
'pingpongputsprite(6,21,130,4,3)
'pingpongputsprite(7,21,150,4,3)
'
'seed=0
'
'for ee2=64 to 1023-64
'  for ee3=1 to 100
'    poke $8400+ee2,$01
'    next:next
'
'ee4=0
'for ee2=3 to 3+15
'  for ee3=15 to 15+15
'    poke $8400+ee3+(ee2*32),ee4
'    ee4=ee4+1
'    next:next
'
'ee4=0
'for ee2=20 to 20+7
'  for ee3=15 to 15+15
'    poke $8400+ee3+(ee2*32),ee4 band 3
'    poke $8000+ee3+(ee2*32),int(ee4/4)
'    ee4=ee4+1
'    next:next
'
'
'
'
'for ee2=4 to 7
'  for ee3=2 to 5
'    poke $8400+ee3+(ee2*32),code("X")
'    next
'    next
'
'test1(377,2445,63888,2342)
'
'eee=$4207
'i=code("Q")
'poke eee,i
'eee=eee+1
'i=code("T")
'poke eee,i
'
'seed=0
'for eee=$4400 to $47FF
'  seed=smsrnd(seed)
'  poke eee,seed
'  next
'
'ex0=80:ey0=176
'
'eee=0
'ecsp=0:ecsq=0
'
'
'do
'poke $A600,255 '- watchdog? - it seems to work! thanks, Jim Bagley!!!!
'
'ej0=peek($A800) bxor $ff
'ej1=peek($A880) bxor $ff
'
'poke ($8400+(10*32)+6) bxor %0000001111100000,ej0
'poke ($8400+(10*32)+7) bxor %0000001111100000,ej1
'
'poke ($8400+(12*32)+8) bxor %0000001111100000,48+(ex0 mod 10)
'poke ($8400+(12*32)+7) bxor %0000001111100000,48+((int(ex0/10)) mod 10)
'poke ($8400+(12*32)+6) bxor %0000001111100000,48+((int(ex0/100)) mod 10)
'
'poke ($8400+(13*32)+8) bxor %0000001111100000,48+(ey0 mod 10)
'poke ($8400+(13*32)+7) bxor %0000001111100000,48+((int(ey0/10)) mod 10)
'poke ($8400+(13*32)+6) bxor %0000001111100000,48+((int(ey0/100)) mod 10)
'
'

